/**
* @program: src
*
* @description:用户登录注册接口
*
* @author: Mr.chen
*
* @create: 2020-12-19 10:56
**/
package handler

import (
	proto "ydd-im/service/account/proto"
	"ydd-im/service/api/define"
	dbcli "ydd-im/service/dbproxy/handle"
	"ydd-im/util/logger"
	"ydd-im/util/response"
	"context"
	"encoding/json"

	"go.uber.org/zap"
)


type Controller struct {
}

// Register : 处理用户注册请求
func (u *Controller) Register(ctx context.Context, req *proto.RegisterUserReq) (res *proto.RegisterUserResp,err error) {
	// 将用户信息注册到用户表中
	res = &proto.RegisterUserResp{} // 返回是指针，必须初始化，否则报错panic
	dbResp, err := dbcli.RegisterIsCluster(req.Username, req.Passwd,req.Userkey,req.Appkey)
	if err != nil { // 返回是指针，必须是指针，否则报错panic
		logger.GetLg().Error("注册失败",
			zap.String("host", define.LocalHost),
			zap.String("port", define.Port),
			zap.Any("err", err.Error()),
		)
		res.Code = response.ErrorCode
		res.Msg = err.Error()
		return res,nil
	}
	logger.GetLg().Info("注册接口返回",
		zap.String("host", define.LocalHost),
		zap.String("port", define.Port),
		zap.String("username",req.Username ),
		zap.String("Passwd",req.Passwd ),
		zap.String("userkey",req.Userkey ),
		zap.String("Appkey",req.Appkey ),
		zap.Int("Code",dbResp.Code ),
		zap.String("Msg",dbResp.Msg ),
	)
	res.Code = int64(dbResp.Code)
	res.Msg = dbResp.Msg
	return res,nil
}

// Login : 处理用户登录请求
func (u *Controller) Login(ctx context.Context, req *proto.LoginUserReq) (res *proto.LoginUserResp,err error) {
	res = &proto.LoginUserResp{} // 返回是指针，必须初始化，否则报错panic
	dbResp, err := dbcli.LoginIsCluster(req.Username, req.Passwd,req.Client,req.Appkey)
	if err != nil { // 返回是指针，必须是指针，否则报错panic
		logger.GetLg().Error("登录失败",
			zap.String("host", define.LocalHost),
			zap.String("port", define.Port),
			zap.Any("err", err.Error()),
		)
		res.Code = response.ErrorCode
		res.Msg = err.Error()
		return res,nil
	}
	data, err := json.Marshal(dbResp.Data)
	logger.GetLg().Info("登录接口返回",
		zap.String("host", define.LocalHost),
		zap.String("port", define.Port),
		zap.String("username",req.Username ),
		zap.String("Passwd",req.Passwd ),
		zap.String("client",req.Client),
		zap.String("Appkey",req.Appkey ),
		zap.Int("Code",dbResp.Code ),
		zap.String("Msg",dbResp.Msg ),
		zap.Any("firstdata",dbResp.Data),
		zap.Any("data",data),
	)
	if err != nil {
		res.Code = response.ErrorCode
		res.Msg = err.Error()
		return res,nil
	}
	res.Code = int64(dbResp.Code)
	res.Msg = dbResp.Msg
	res.Data = data
	return res,nil

}

//  GetUserInfoByToken:处理检查用户token有效性请求
func (u *Controller) GetUserInfoByToken(ctx context.Context, req *proto.UserInfoByTokenReq) (res *proto.UserInfoByTokenResp,err error) {
	res = &proto.UserInfoByTokenResp{} // 返回是指针，必须初始化，否则报错panic
	dbResp, err := dbcli.GetUserInfoByTokenIsCluster(req.Token)
	if err != nil { // 返回是指针，必须是指针，否则报错panic
		logger.GetLg().Error("检查token有效性失败",
			zap.String("host", define.LocalHost),
			zap.String("port", define.Port),
			zap.Any("err", err.Error()),
		)
		res.Code = response.ErrorCode
		res.Msg = err.Error()
		return res,nil
	}
	data, err := json.Marshal(dbResp.Data)
	logger.GetLg().Info("GetUserInfoByToken接口返回",
		zap.String("host", define.LocalHost),
		zap.String("port", define.Port),
		zap.String("token",req.Token ),
		zap.Int("Code",dbResp.Code ),
		zap.String("Msg",dbResp.Msg ),
		zap.Any("firstdata",dbResp.Data),
		zap.Any("data",data),
	)
	if err != nil {
		res.Code = response.ErrorCode
		res.Msg = err.Error()
		return res,nil
	}
	res.Code = int64(dbResp.Code)
	res.Msg = dbResp.Msg
	res.Data = data
	return res,nil

}